<template>
  <div class="clock">
    <div class="center" :style="{ width: diameter + 'px', height: diameter + 'px' }">
      <ul class="line-min">
        <li v-for="x in 60" :key="x"
          :style="{ transform: `rotate(${6 * x}deg)  translate(${diameter / 2 - 8}px,50%)` }">
        </li>
      </ul>
      <ul class="line-hour">
        <li v-for="x in 12" :key="x"
          :style="{ transform: `rotate(${30 * x}deg)  translate(${diameter / 2 - 16}px,50%)` }">
        </li>
      </ul>
      <ul class="clocknum" :style="{ width: (diameter - 60) + 'px', height: (diameter - 60) + 'px' }">
        <li v-for="x in 12" :key="x" :style="{ left: getNumber(x).left + 'px', top: getNumber(x).top + 'px' }">{{ x }}
        </li>
      </ul>
      <ul class="point">
        <li class="hour" :style="{ width: (diameter * 0.2) + 'px', transform: `rotate(${deghour}deg)` }"></li>
        <li class="min" :style="{ width: (diameter * 0.28) + 'px', transform: `rotate(${degmin}deg)` }"></li>
        <li class="sec" :style="{ width: (diameter * 0.36) + 'px', transform: `rotate(${degsec}deg)` }">
        </li>
        <li class="circle"></li>
      </ul>
    </div>
    <div class="num-clock" v-if="hours != 0">
      {{ `${hours} : ${minute} : ${second}` }}
    </div>
    <!-- <div :style="{ width: 300 + 'px' }" class="step">
      <el-slider v-model="progress" :step="10" show-stops @change="changeDiameter" />
    </div> -->
  </div>
</template>

<script setup>
import { onMounted, onUnmounted, ref } from 'vue';
let timer = null;
const degsec = ref('')
const degmin = ref('')
const deghour = ref('')
const diameter = ref(300)
const progress = ref(0)
const second = ref(0)
const minute = ref(0)
const hours = ref(0)
function reset() {
  return new Promise((resolve) => {
    let time = new Date();
    let sec = time.getSeconds();
    let min = time.getMinutes();
    let hour = time.getHours();
    let subsec = (360 / 60);//秒针1s旋转6度
    let submin = (360 / 3600);//分针1s旋转0.1度
    let subhour = (360 / 12 / 3600);//时针1s旋转360/12/3600度
    degsec.value = (subsec * sec - 90) % 360;//(6*s-90)%360
    degmin.value = (submin * (min * 60 + sec) - 90) % 360; //(0.1*(m*60+s)-90)360%
    deghour.value = (subhour * (hour * 3600 + min * 60 + sec) - 90) % 360; //防止超过360度的情况
    resolve()
  })
}
function getNumber(i) {
  const r = (diameter.value - 60) / 2;//半径
  const radian = (2 * Math.PI / 360) * (i * 30 - 90);//通过角度求弧度  i*30-90是角度
  let left = r * (1 + Math.cos(radian));
  let top = r * (1 + Math.sin(radian));
  return {
    left, top
  }
}

function zero(num) {
  return num < 10 ? '0' + num : num
}

function setTime() {
  let time = new Date();
  second.value = zero(time.getSeconds())
  minute.value = zero(time.getMinutes())
  hours.value = zero(time.getHours())
}

function createTime() {
  degsec.value += (360 / 60);
  degmin.value += (360 / 3600);
  deghour.value += (360 / 12 / 3600);
  degsec.value = degsec.value >= 360 ? 0 : degsec.value;
  degmin.value = degmin.value >= 360 ? 0 : degmin.value;
  deghour.value = deghour.value >= 360 ? 0 : deghour.value;
  setTime()
}
function changeDiameter(e) {
  progress.value = e
  diameter.value = 300 + (100 / 100 * e)
}

onMounted(() => {
  reset().then(() => {
    //写入样式
    // const sheet = document.styleSheets[11]; // 获取样式表对象
    // const keyframesRule = `@keyframes rotate {form{transform: rotate(${degsec.value + 0} deg)};to {transform: rotate(${degsec.value + 360} deg);}`;
    // // 定义keyframes规则
    // sheet.insertRule(keyframesRule, 1); // 添加keyframes规则到样式表
    // // console.log(document.styleSheets[11])
  })

  timer = setInterval(() => {
    createTime()
  }, 1000);

});


// onMounted(() => {
//   reset()
//   timer = setInterval(() => {
//     createTime()
//   }, 1000);
// });
onUnmounted(() => {
  clearInterval(timer);
});
</script>

<style scoped lang="scss">
.num-clock {
  text-align: center;
  font-size: 64px;
  font-weight: 700;
}

.step {
  margin-left: 50px;
}

.clock {
  width: 100%;
  height: 100%;
}

.center {
  background: #292a38;
  border-radius: 50%;
  position: relative;
  border: 2px solid #fff;
  margin: 0 auto;
}

/*表盘数字*/
.clocknum {
  position: relative;
  top: 50%;
  left: 50%;
  margin: 0;
  padding: 0;
  transform: translate(-50%, -50%);
}

.clocknum li {
  position: absolute;
  top: 50%;
  left: 50%;
  font-size: 15px;
  transform: translate(-50%, -50%);
  color: #fff;
  transform-origin: left center;
  font-size: 24px;
  font-weight: 700;
}

/*刻度*/
.line-hour li,
.line-min li {
  position: absolute;
  top: 50%;
  left: 50%;
  background: #fff;
  transform-origin: left center;
  /*transform-origin设置旋转元素的基点位置
  * transform前li的左侧顶点是时钟的中心
  * 如果不设置transform-origin 默认是center center 则是以偏移前li的中心点而非左侧顶点进行旋转。整个表盘会偏移
  * */

}

.line-hour li {
  width: 10px;
  height: 2px;
}

.line-min li {
  width: 5px;
  height: 2px;
}

/*指针*/
.point li {
  position: absolute;
  top: 50%;
  left: 50%;
  background: #fff;
  transform-origin: left center;
}

.point li.circle {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  transform: translate(-50%, -50%);
}

.point li.hour {
  width: 45px;
  height: 5px;
  background-color: red;
  border-radius: 2px;
}

.point li.min {
  width: 60px;
  height: 4px;
  background-color: green;
  border-radius: 2px;
}

.point li.sec {
  width: 80px;
  height: 3px;
  background-color: blue;
  border-radius: 2px;
  animation: rotate 60s linear infinite;
}

// @keyframes rotate {
//   from {
//     transform: rotate(0deg);
//   }
//   to {
//     transform: rotate(360deg);
//   }
// }
</style>
